#include <bits/stdc++.h>
#define N 12
#define x first
#define y second
using namespace std;

typedef long long LL;
bool f[N][N][530];
int w[N][N];
int n, m;

int main()
{
	while (cin >> n >> m)
	{
		if (n == 0 && m == 0) break;
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; ++i)
			for (int j = 1; j <= m; ++j)
				cin >> w[i][j];

		f[0][1][0 + 300] = 1;
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				for (int v = 100; v <= 500; ++v)
				{
					f[i][j][v] |= f[i][j - 1][v - w[i][j]] | f[i - 1][j][v - w[i][j]];
				}
			}
		}
		int ans = INT_MAX;
		for (int i = 301; i <= 500; ++i)
		{
			if (f[n][m][i])
			{
				ans = i - 300;
				break;
			}
		}
		if (ans == INT_MAX) cout << -1 << endl;
		else cout << ans << endl;
	}
	return 0;
}
